﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using WpfApplication2.UserControls;
using WpfApplication2.Helpers;
using System.Windows.Input;
using System.Windows;
using WpfApplication2.SindicatoWebService;
using WpfApplication2.Views.TipoOrden;

namespace WpfApplication2.ViewModels.TipoOrden
{
    public class BuscarTipoOrdenViewModel : ViewModel
    {
        public Instancias Instancias { get; set; }

        #region properties

        public SindicatoWebService.Service1SoapClient WS;
        public Frame ContainerFrame
        {
            get;
            set;
        }
        public ModalDialog modalDialog
        {
            get;
            set;
        }
        public BajaAfiliadoDialog bajaAfiliadoDialog
        {
            get;
            set;
        }

        
        public string Nombre
        {
            get
            {
                return nombre;
            }
            set
            {
                if (nombre != value)
                {
                    nombre = value;
                    RaisePropertyChanged("Nombre");
                }
            }
        }
        private string nombre;

       
        public TipoOrdenDTO[] Resultados
        {
            get
            {
                return resultados;
            }
            set
            {
                if (resultados != value)
                {
                    resultados = value;
                    RaisePropertyChanged("Resultados");
                }
            }
        }
        private TipoOrdenDTO[] resultados;


        public TipoOrdenDTO TipoOrden
        {
            get
            {
                return tipoOrden;
            }
            set
            {
                if (tipoOrden != value)
                {
                    tipoOrden = value;
                    RaisePropertyChanged("Afiliado");
                }
            }
        }
        private TipoOrdenDTO tipoOrden;


        public Visibility ResultsVisibility
        {
            get
            {
                return resultsVisibility;
            }
            set
            {
                if (resultsVisibility != value)
                {
                    resultsVisibility = value;
                    RaisePropertyChanged("ResultsVisibility");
                }
            }
        }
        private Visibility resultsVisibility;

        public Visibility ResultsListVisibility
        {
            get
            {
                return resultsListVisibility;
            }
            set
            {
                if (resultsListVisibility != value)
                {
                    resultsListVisibility = value;
                    RaisePropertyChanged("ResultsListVisibility");
                }
            }
        }
        private Visibility resultsListVisibility;

        public Visibility NoResultsVisibility
        {
            get
            {
                return noResultsVisibility;
            }
            set
            {
                if (noResultsVisibility != value)
                {
                    noResultsVisibility = value;
                    RaisePropertyChanged("NoResultsVisibility");
                }
            }
        }
        private Visibility noResultsVisibility;

        


        #endregion

        public BuscarTipoOrdenViewModel(Instancias instancias)
        {
            WS = new Service1SoapClient();
            Instancias = instancias;
            ResultsVisibility = Visibility.Visible;
            Buscarto();
        }


        #region Buscar Tipo Orden
        
        private bool CanExecute()
        {
            return true;
        }


        private DelegateCommand buscarCommand;

        public ICommand BuscarCommand
        {
            get
            {
                if (buscarCommand == null)
                {
                    buscarCommand = new DelegateCommand(Buscar, CanExecute);
                }
                return buscarCommand;
            }
        }

       
        private void Buscar()
        {
            if (string.IsNullOrEmpty(Nombre))
            {
                Instancias.ModalDialog.ShowHandlerDialog(String.Format("Debe ingresar un nombre de plan"));
            }
            else
            {
                Resultados = WS.BuscarTipoOrden(Nombre);
                   
                ResultsVisibility = Visibility.Visible;
                if (Resultados.Count() > 0)
                {
                    ResultsListVisibility = Visibility.Visible;
                    NoResultsVisibility = Visibility.Collapsed;
                }
                else
                {
                    ResultsListVisibility = Visibility.Collapsed;
                    NoResultsVisibility = Visibility.Visible;
                }
            }
        }

        
        private DelegateCommand buscarCommandTO;

        public ICommand BuscarCommandTO
        {
            get
            {
                if (buscarCommandTO == null)
                {
                    buscarCommandTO = new DelegateCommand(Buscarto, CanExecute);
                }
                return buscarCommandTO;
            }
        }


        private void Buscarto()
        {
         Resultados = WS.GetTiposOrden();
        }
        #endregion


        #region Editar Command

        private DelegateCommand editarCommand;

        public ICommand EditarCommand
        {
            get
            {
                if (editarCommand == null)
                {
                    editarCommand = new DelegateCommand(Editar, CanExecute);
                }
                return editarCommand;
            }
        }

        private void Editar()
        {
            if (TipoOrden != null)
            {
                Instancias.FrameContent.Content = new EditarTipoOrden(Instancias, TipoOrden);
            }
            else
            {
                Instancias.ModalDialog.ShowHandlerDialog(String.Format("Seleccione un Tipo de Orden por favor"));
            }
            
        }

        #endregion

    }
}

    

